﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Elective1_kim_sequel
{
    public partial class Form_Add : Form
    {
        public Form_Add()
        {
            InitializeComponent();
            btn_ok.Click += new EventHandler(btn_ok_Click);
            btn_cancel.Click += new EventHandler(btn_cancel_Click);
            comboBox1.SelectedIndex = 1;
        }

        void Database_Query_Completed(object sender, AsyncCompletedEventArgs e)
        {
            Database.Query_Completed -= new AsyncCompletedEventHandler(Database_Query_Completed);
            MessageBox.Show(" Record Added! ");
            this.Close();
        }

        void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void btn_ok_Click(object sender, EventArgs e)
        {
            string lname = txt_lname.Text.ToString();
            string fname = txt_fname.Text.ToString();
            string mname = txt_mname.Text.ToString();
            string user = txt_user.Text.ToString();
            string pwd = txt_pass.Text.ToString();
            string accesslvl = "0";
            if (comboBox1.Text == "Admin")
            {
                accesslvl = "1";
            }

            else if (comboBox1.Text == "Admin")
            {
                accesslvl = "0";
            }

            if (lname.Trim() != "" || fname.Trim() != "" || mname.Trim() != "" || user.Trim() != "" || pwd.Trim() != "")
            {
                Database.Query_Completed += new AsyncCompletedEventHandler(Database_Query_Completed);
                Database.Query_Async("INSERT INTO tbl1 (Last_Name, First_Name, Middle_Name, User_Name, Password, AccessLevel) VALUES ('" + lname + "','" + fname + "','" + mname + "','" + user + "','" + pwd + "','" + accesslvl + "')");
            }

            else { MessageBox.Show("Please fill all the fields!", "OOPS!", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }
    }
}
